import {Controller, SourceRsp} from "../type/interfaces.ts";
import { handle } from "../vendor/handle.ts";
import {mainWindow} from "../windows/main-window.ts";
import {IpcMainEvent} from "electron";
import axios from "axios";
import {API_HOST} from "../type/const.js";
import {getIpInfo} from "../helper/utils.js";
import {searchView} from "../windows/view/search-view.js";


export default class PlayerController implements Controller {
  @handle()
  async playVideo(e: IpcMainEvent, data: SourceRsp) {
    const req: any = {}
    req.title = data.title
    req.url = data.documentURL
    req.version = process.env.APP_VERSION

    const ipInfo = await getIpInfo()
    req.ip = ipInfo.ip
    req.location = ipInfo.addr
    req.token = data.token
    try{
      const resp = await axios.post(API_HOST + "/playLog", req);
      if (resp.data.code === 0) {
        mainWindow.send("send-to-play", data)
      }else{
        mainWindow.send("error-info", resp.data.msg)
      }
    }catch (e) {
      mainWindow.send("error-info",  (e as any).message)
    }
  }

  @handle()
  async clearUrlCache() {
    searchView.clearUrlCache()
  }
}

export const playerController = new PlayerController()
